/////////////////////////////////////////////////////////////////////////////
//
//  Fifteen Puzzle
//  Kolonitsky Developers Alliance (c) 2012
//  All rights received
//
/////////////////////////////////////////////////////////////////////////////

package view.states
{

    import caurina.transitions.Tweener;

    import com.adobe.net.proxies.RFC2817Socket;

    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;

    import flashx.textLayout.formats.TextAlign;

    import model.types.DirectionName;

    import model.vo.PuzzleVO;

    import org.kolonitsky.alexey.ui.SimpleButton;

    import org.kolonitsky.alexey.utils.DateUtil;

    import view.skins.PuzzleSkin;

    public class PuzzleState implements IState
    {
        public function PuzzleState()
        {
            _skin = new PuzzleSkin();
        }




        //-------------------------------------------------------------------
        //
        //  Implement IState interface
        //
        //-------------------------------------------------------------------

        public function displayGame(game:PuzzleVO):void
        {
            _game = game;

            //Change tile position
            var x0:Number = Config.BOARD_LEFT;
            var y0:Number = Config.BOARD_TOP;

            for (var i:int = 0; i < Config.BOARD_CELLS; i++)
            {
                var colIndex:int = i % Config.BOARD_WIDTH;
                var rowIndex:int = int((i - colIndex) / Config.BOARD_WIDTH);
                var number:uint = game.fields[i];
                var tile:Bitmap = _numbers[number];
                Tweener.addTween(tile, {
                    x: x0 + colIndex * Config.TILE_WIDTH,
                    y: y0 + rowIndex * Config.TILE_HEIGHT,
                    rotation: 0.0,
                    alpha: 1.0,
                    time: Config.TILE_ANIMATION_SPEED
                });

                if (number == Config.EMPTY_CELL_CODE)
                {
                    _skin.moveJoystik(tile.x,  tile.y);
                    _skin.availableDirection(getAvailableDirections());
                }
            }

            //Update duration field
            _skin.duration = game.duration;
        }


        public function activate():void
        {
        }

        //-----------------------------
        // skin
        //-----------------------------

        private var _skin:PuzzleSkin;

        public function get skin():Sprite
        {
            return _skin as Sprite;
        }


        //-----------------------------
        //  numbers
        //-----------------------------

        private var _numbers:Vector.<Bitmap>;

        public function get numbers():Vector.<Bitmap>
        {
            return _numbers;
        }

        public function set numbers(value:Vector.<Bitmap>):void
        {
            _numbers = value;
        }



        //-------------------------------------------------------------------
        //
        //  Private
        //
        //-------------------------------------------------------------------

        private var _game:PuzzleVO;

        private function getAvailableDirections():Array
        {
            var emptyCellIndex:int = _game.fields.indexOf(Config.EMPTY_CELL_CODE);
            var result:Array = [];
            var colIndex:int = int(emptyCellIndex % Config.BOARD_WIDTH);
            var rowIndex:int = int((emptyCellIndex - colIndex) / Config.BOARD_WIDTH);

            if (rowIndex > 0)
                result.push(DirectionName.TOP);
            if (colIndex < Config.BOARD_WIDTH - 1)
                result.push(DirectionName.RIGHT);
            if (rowIndex < Config.BOARD_HEIGHT - 1)
                result.push(DirectionName.BOTTOM);
            if (colIndex > 0)
                result.push(DirectionName.LEFT);

            return result;
        }

    }
}
